<?php

namespace app\modules\Order\models;

use Yii;
use Overtrue\Pinyin\Pinyin;
use yii\web\UploadedFile;

/**
 * This is the model class for table "leader".
 *
 * @property integer $id
 * @property string $first_name
 * @property string $last_name
 * @property string $en_first_name
 * @property string $en_last_name
 * @property string $code
 * @property string $pinyin
 * @property integer $gender
 * @property string $birthday
 * @property string $country
 * @property string $passport
 * @property string $passport_add
 * @property string $passport_date
 * @property integer $passport_type
 * @property string $phone
 * @property string $remark
 * @property integer $status
 * @property integer $user_id
 * @property string $created_at
 * @property string $updated_at
 */
class Leader extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'leader';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['gender', 'passport_type', 'status', 'user_id'], 'integer'],
            [['birthday', 'passport_date','passport_grant','created_at', 'updated_at'], 'safe'],
            [['first_name', 'last_name', 'en_first_name', 'en_last_name'], 'string', 'max' => 32],
            [['code', 'country', 'phone','native'], 'string', 'max' => 64],
            [['pinyin', 'passport', 'passport_add', 'remark','passport_img'], 'string', 'max' => 255],
        ];
    }


    // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }


        //  图片上传
        $upload = UploadedFile::getInstance($this, "passport_img");

        if($upload){
            $dir = sprintf("uploads/passport/%s",date("Ym"));

            if(!is_readable($dir)) {
                is_file($dir) or mkdir($dir,0755);
            }

            $suffix = $upload->extension;

            $path = sprintf('%s/%s.%s', $dir, uniqid(true), $suffix);

            $ret = $upload->saveAs($path);
            if($ret){
                $this->passport_img = $path;
            }
        }else{
            unset($this->passport_img);
        }
         
         
        // 生成拼音 
        $name = sprintf("%s %s", $this->first_name, $this->last_name);
        $this->pinyin = Pinyin::trans($name,['accent'=>false]);

        return parent::beforeSave($insert);
    }//}}}
    

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.order', 'ID'),
            'first_name' => Yii::t('app.order', 'First Name'),
            'last_name' => Yii::t('app.order', 'Last Name'),
            'en_first_name' => Yii::t('app.order', 'En First Name'),
            'en_last_name' => Yii::t('app.order', 'En Last Name'),
            'code' => '领队证号',
            'pinyin' => Yii::t('app.order', 'Pinyin'),
            'gender' => Yii::t('app.order', 'Gender'),
            'birthday' => Yii::t('app.order', 'Birthday'),
            'country' => Yii::t('app.order', 'Country'),
            'passport' => Yii::t('app.order', 'Passport'),
            'passport_add' => Yii::t('app.order', 'Passport Add'),
            'passport_date' => Yii::t('app.order', 'Passport Date'),
            'passport_type' => Yii::t('app.order', 'Passport Type'),
            'phone' => Yii::t('app.order', 'Phone'),
            'remark' => Yii::t('app.order', 'Remark'),
            'status' => Yii::t('app.order', 'Status'),
            'user_id' => Yii::t('app.order', 'User ID'),
            'created_at' => Yii::t('app.order', 'Created At'),
            'updated_at' => Yii::t('app.order', 'Updated At'),
            'passport_img' => Yii::t('app.order', 'Passport Img'),
            'native' => Yii::t('app.order', 'Native'),
            'passport_grant' => Yii::t('app.order', 'Passport Grant'),
        ];
    }
}
